package com.jiu.ui;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JTextPane;
import javax.swing.KeyStroke;

import com.jiu.core.FishContext;

public class TextPaneMenu extends JTextPane implements MouseListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = -8738710750554104638L;

	private JPopupMenu pop = null; // 弹出菜单
	private FishContext context;
	private JMenuItem copy1 = null,copy2 = null, paste = null, cut = null,clear = null; // 三个功能菜单
	private Image backImage = null;
	private long clickTime;

	public TextPaneMenu(FishContext context) {
		this.context = context;
		init();
	}
    @Override
    public   void   paintComponent   (Graphics   g)   {
    	for(int i=0;i<40;i++)
    		g.drawImage(backImage,0,i*1200,this);
    	super.paintComponent(g);
    }
	/**
	 * 这里不用粘贴和剪切
	 */
	private void init() {
		this.setOpaque(false);
		try {
			backImage = ImageIO.read(getClass().getResourceAsStream("image/background.jpg"));
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		addMouseListener(this);
		pop = new JPopupMenu();
		pop.add(copy1 = new JMenuItem("复制到输入框"));
		pop.add(copy2 = new JMenuItem("复制"));
//		pop.add(paste = new JMenuItem("粘贴"));
//		pop.add(cut = new JMenuItem("剪切"));
		pop.add(clear = new JMenuItem("清空"));
		copy2.setAccelerator(KeyStroke.getKeyStroke('C', InputEvent.CTRL_MASK));
//		paste.setAccelerator(KeyStroke.getKeyStroke('V', InputEvent.CTRL_MASK));
//		cut.setAccelerator(KeyStroke.getKeyStroke('X', InputEvent.CTRL_MASK));
		
		copy1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				action(e);
			}
		});
		copy2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				action(e);
			}
		});
//		paste.addActionListener(new ActionListener() {
//			public void actionPerformed(ActionEvent e) {
//				action(e);
//			}
//		});
//		cut.addActionListener(new ActionListener() {
//			public void actionPerformed(ActionEvent e) {
//				action(e);
//			}
//		});
		clear.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				action(e);
			}
		});
		this.add(pop);
	}

	/**
	 * 菜单动作
	 * 
	 * @param e
	 */
	public void action(ActionEvent e) {
		String str = e.getActionCommand();
		if (str.equals(copy1.getText())) { // 复制到输入框
			this.copy();
			JTextPane sendText = context.getMainFrame().getSendText();
			sendText.setText(sendText.getText()+" ");
			sendText.paste();
		}else if (str.equals(copy2.getText())){// 复制
			this.copy();
		}else if (str.equals(clear.getText())){//清空
			this.setText("");
		}else if (str.equals(paste.getText())) { // 粘贴
			this.paste();
		}else if (str.equals(cut.getText())) { // 剪切
			this.cut();
		}
	}

	/**
	 * 剪切板中是否有文本数据可供粘贴
	 * 
	 * @return true为有文本数据
	 */
	public boolean isClipboardString() {
		boolean b = false;
		Clipboard clipboard = this.getToolkit().getSystemClipboard();
		Transferable content = clipboard.getContents(this);
		try {
			if (content.getTransferData(DataFlavor.stringFlavor) instanceof String) {
				b = true;
			}
		} catch (Exception e) {
		}
		return b;
	}

	/**
	 * 文本组件中是否具备复制的条件
	 * 
	 * @return true为具备
	 */
	public boolean isCanCopy() {
		boolean b = false;
		int start = this.getSelectionStart();
		int end = this.getSelectionEnd();
		if (start != end)
			b = true;
		return b;
	}

	@Override 
	public void mousePressed(MouseEvent e) {
		if (e.getButton() == MouseEvent.BUTTON3) {
			copy1.setEnabled(isCanCopy());
			copy2.setEnabled(isCanCopy());
//			paste.setEnabled(isClipboardString());
//			cut.setEnabled(isCanCopy());
			pop.show(this, e.getX(), e.getY());
		}else if(e.getButton() == MouseEvent.BUTTON1&&isDoubleClick()){
			this.copy();
			JTextPane sendText = context.getMainFrame().getSendText();
			sendText.setText(sendText.getText()+" ");
			sendText.paste();
		}
	}
	private boolean isDoubleClick(){
		boolean flag = false;
		long nowTime = System.currentTimeMillis();
		if((nowTime-clickTime)<300){
			flag = true;
		}
		clickTime = nowTime;
		return flag;
	}
	@Override
	public void mouseClicked(MouseEvent e) {		
	}
	@Override
	public void mouseReleased(MouseEvent e) {			
	}
	@Override
	public void mouseEntered(MouseEvent e) {	
		
	}
	@Override
	public void mouseExited(MouseEvent e) {				
	}

}
